<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Index extends Controller
{
    public function _initialize()
    {

//        1:百度
//        2:360
//        3:百度移动端
//        4:sogou
//        5:神马
//      1010://百度
//		1015://360
//		7010://百度移动
//		1030://搜狗
//		7070://神马
//		7015://360移动
//      7030://搜狗移动
//		7080: //头条

    }


//计划任务


//redis test
    public function set_redis()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->delete("name"); //先清空
        $rows = Db::table('tb_webkey')->where(['state' => 1, 'nowPm' => ['between', [1, 20]]])->order('id desc')->limit(3)->select();
        foreach ($rows as $k => $v) {
            $redis->lpush('name', json_encode($v));
        }

        $arList = $redis->lrange("name", 0, -1);
        dump(($arList));
    }


    public function get_redis()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);

//        $arList = $redis->lPop('name'); //输出并删除
        $arList = $redis->lrange("keList", 0, -1);
        if ($arList) {
            dump('keList');
            dump($arList);
        } else {
            dump('2');
        }
//        dump(json_decode($arList));
//        dump($arList[0]);
//        dump(json_decode($arList[0]));
    }


    public function index()
    {

        return $this->fetch();
    }

//    封顶超出的返还
    public function returnTopMoney()
    {
        $rows = Db::table('tb_Money m')
            ->field('u.money umoney,u.moneypay upay,w.istop,w.topMoney,w.nowtop,k.moneypay,k.iskf,m.*')
            ->join('tb_user u', 'u.id=m.userid')
            ->join('tb_webinfo w', 'w.id= m.webid')
            ->join('tb_webkey k', 'k.id= m.keyid')
            ->where([
                'm.keyid' => ['gt', 0],
                'w.nowtop' => ['gt', Db::raw('topMoney')]
            ])
            ->whereTime('m.intime', 'today')
            ->select();

        foreach ($rows as $k => $v) {
            $webinfo = Db::table('tb_webinfo')->field('topMoney,nowtop')->where(['id' => $v['webid'],])->find();

            if ($webinfo['nowtop'] > $v['topMoney']) {
                print_r('封顶：' . $v['topMoney'] . '--扣除' . $webinfo['nowtop'] . '需要返还<br>');
//                减去封顶 当前扣费 -
                Db::table('tb_webinfo')->where(['id' => $v['webid'], 'istop' => 1])->setDec('nowtop', $v['money']);

                //          更新该词 消费金额 - ，扣费状态还原
                Db::table('tb_webkey')->where(['id' => $v['keyid']])->update(['moneypay' => Db::raw('moneypay-' . $v['money']), 'iskf' => 0,]);

//              更新 用户余额 + ，消费金额 -
                Db::table('tb_user')->where('id', $v['userID'])->update([
                    'moneypay' => Db::raw('moneypay-' . $v['money']),
                    'money' => Db::raw('money+' . $v['money'])
                ]);

                //          tb_money 表 删除 当前 数据
                Db::table('tb_money')->where('id', $v['id'])->delete();
            }
//

//////


        }
//        dump($rows);
    }


//    更新关键词 sid
    public function updatekey()
    {

        $rows = Db::table('tb_webkey')
            ->where([
                'sid' => 0,
                'state' => 1 //正常词
            ])->select();

        foreach ($rows as $k => $v) {
            if ($rows[$k]['typeid'] == 1) {
//                $rows[$k]['sid'] =1010;
                Db::table('tb_webkey')->where(['typeid' => 1, 'id' => $rows[$k]['id']])->update(['sid' => 1010]);
            } elseif ($rows[$k]['typeid'] == 2) {
//                $rows[$k]['sid'] =1015;
                Db::table('tb_webkey')->where(['typeid' => 2, 'id' => $rows[$k]['id']])->update(['sid' => 1015]);

            } elseif ($rows[$k]['typeid'] == 3) {
//                $rows[$k]['sid'] =7010;
                Db::table('tb_webkey')->where(['typeid' => 3, 'id' => $rows[$k]['id']])->update(['sid' => 7010]);

            } elseif ($rows[$k]['typeid'] == 4) {
//                $rows[$k]['sid'] =1030;
                Db::table('tb_webkey')->where(['typeid' => 4, 'id' => $rows[$k]['id']])->update(['sid' => 1030]);
            } elseif ($rows[$k]['typeid'] == 5) {
//                $rows[$k]['sid'] =7070;
                Db::table('tb_webkey')->where(['typeid' => 5, 'id' => $rows[$k]['id']])->update(['sid' => 7070]);
            }

        }
        return json($rows);
    }




}
